package com.freshmarket.freshmarket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.freshmarket.freshmarket.controller.result.ShoppingCartResult;
import com.freshmarket.freshmarket.controller.result.ShoppingCartUserCouponsResult;
import com.freshmarket.freshmarket.dao.*;
import com.freshmarket.freshmarket.entity.Coupon;
import com.freshmarket.freshmarket.entity.Product;
import com.freshmarket.freshmarket.entity.ShoppingCar;
import com.freshmarket.freshmarket.entity.UserCoupon;
import com.freshmarket.freshmarket.service.IShoppingCarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Squirrel
 * @since 2020-09-07
 */
@Service
public class ShoppingCarServiceImpl extends ServiceImpl<ShoppingCarMapper, ShoppingCar> implements IShoppingCarService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ShoppingCarMapper shoppingCarMapper;
    @Autowired
    private UserCouponMapper userCouponMapper;
    @Autowired
    private CouponMapper couponMapper;

    @Override
    public List<ShoppingCartResult> updateShoppingCart(Integer userId, Integer productId, Integer productNum) {
        UpdateWrapper<ShoppingCar> shoppingCarUpdateWrapper = new UpdateWrapper<>();
        shoppingCarUpdateWrapper.eq("user_id", userId).eq("product_id",productId).set("product_num", productNum);
        shoppingCarMapper.update(null, shoppingCarUpdateWrapper);
        return getShoppingCart(userId);
    }

    //查询购物车表并查询对应商品信息
    @Override
    public List<ShoppingCartResult> getShoppingCart(Integer userId) {
        ArrayList<ShoppingCartResult> shoppingCartResults = new ArrayList<>();
        QueryWrapper<ShoppingCar> shoppingCarQueryWrapper = new QueryWrapper<>();
        shoppingCarQueryWrapper.eq("user_id", userId).gt("product_num",0);
        List<ShoppingCar> shoppingCars = shoppingCarMapper.selectList(shoppingCarQueryWrapper);
        if (shoppingCars.size() > 0) {
            for (ShoppingCar shoppingCar : shoppingCars) {
                ShoppingCartResult shoppingCartResult = new ShoppingCartResult();
                Product product = productMapper.selectById(shoppingCar.getProductId());
                shoppingCartResult.setProductId(product.getProductId());
                shoppingCartResult.setImg(product.getProductImgUrl());
                shoppingCartResult.setProductName(product.getProductName());
                shoppingCartResult.setProductNum(shoppingCar.getProductNum());
                shoppingCartResult.setProductPrice(product.getProductPrice());
                shoppingCartResults.add(shoppingCartResult);
            }
        }
        return shoppingCartResults;
    }

    public List<ShoppingCartUserCouponsResult> getCoupons(Integer userId) {
        ArrayList<ShoppingCartUserCouponsResult> userCouponsResults = new ArrayList<>();
        QueryWrapper<UserCoupon> userCouponQueryWrapper = new QueryWrapper<>();
        userCouponQueryWrapper.eq("user_id", userId);

        List<UserCoupon> userCoupons = userCouponMapper.selectList(userCouponQueryWrapper);
        for (UserCoupon userCoupon : userCoupons) {
            ShoppingCartUserCouponsResult couponsResult = new ShoppingCartUserCouponsResult();
            couponsResult.setUserId(userId);
            Coupon coupon = couponMapper.selectById(userCoupon.getCouponId());
            couponsResult.setCouponId(coupon.getCouponId());
            couponsResult.setCouponDiscount(coupon.getCouponDiscount());
            if(coupon.getCouponThreshold()==null ){
                continue;
            }
            couponsResult.setCouponThreshold(coupon.getCouponThreshold());
            couponsResult.setCouponPastdue(coupon.getCouponPastdue());
            couponsResult.setUserCouponNum(userCoupon.getUserCouponNum());
            couponsResult.setCouponName(coupon.getCouponName());
            userCouponsResults.add(couponsResult);
        }
        return userCouponsResults;
    }

    @Override
    public void addCar(ShoppingCar shoppingCar) {
        //前端新增商品信息,判断是否之前在购物车存在,若存在,只修改剩余数量
        Integer productId = shoppingCar.getProductId();
        QueryWrapper<ShoppingCar> shoppingCarQueryWrapper = new QueryWrapper<>();
        shoppingCarQueryWrapper.eq("user_id",shoppingCar.getUserId()).eq("product_id",productId);

        List<ShoppingCar> shoppingCars = shoppingCarMapper.selectList(shoppingCarQueryWrapper);
        for (ShoppingCar car : shoppingCars) {
            if(car.getProductId().equals(productId)){
                updateShoppingCart(shoppingCar.getUserId(),productId,shoppingCar.getProductNum()+car.getProductNum());
                return;
            }
        }
        //若不存在则新增一条购物车数据
        shoppingCarMapper.insert(shoppingCar);
    }

}
